package org.example.struct.array;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author liyishan
 * @date 2024/4/1 09:33
 * @apiNote
 */

public class Test02810FinalString {
    public static String finalString(String s) {
        StringBuilder res = new StringBuilder();
        for (Character ch : s.toCharArray()) {
            if (!ch.equals('i')) {
                res.append(ch);
            } else {
                res.reverse();
            }
        }
        return res.toString();
    }

    public static String finalString1(String s) {
        Deque<Character> deque = new ArrayDeque<>();
        boolean isHead = false;
        for(char c:s.toCharArray()){
            if (c =='i'){
                isHead = !isHead;
            }else if (isHead){
                deque.offerFirst(c);
            }else{
                deque.offerLast(c);
            }
        }
        StringBuilder ans = new StringBuilder();
        if(isHead){
            while(!deque.isEmpty()){
                ans.append(deque.pollLast());
            }
        }else{
            while(!deque.isEmpty()){
                ans.append(deque.pollFirst());
            }
        }
        return ans.toString();
    }


}
